package com.hrb.monkey.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hrb.monkey.R;

public class CustomTitleView extends RelativeLayout {

    private Button mleftBtn;
    private Button mrightBtn;
    private TextView mtextView;
    public CustomTitleView(Context context) {
        super(context);
    }

    public CustomTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.mtitle_layout,this,true);
        mleftBtn=findViewById(R.id.title_bar_left);
        mtextView=findViewById(R.id.title_bar_title);
        mrightBtn=findViewById(R.id.title_bar_right);
        TypedArray attributes =context.obtainStyledAttributes(attrs,R.styleable.CustomTitleBar);
        if(attributes!=null){
            //设置背景颜色
            int titleBarGround=attributes.getResourceId(R.styleable.CustomTitleBar_title_background_color,Color.RED);
            setBackgroundResource(titleBarGround);
            //控制是否要显示左侧按钮
            boolean leftBtnVisible =attributes.getBoolean(R.styleable.CustomTitleBar_left_button_visible,true);
            if(leftBtnVisible){
                mleftBtn.setVisibility(View.VISIBLE);
            }else{
                mleftBtn.setVisibility(View.INVISIBLE);
            }
            //设置左边按钮的文字
            String leftButtonText = attributes.getString(R.styleable.CustomTitleBar_left_button_text);
            if(!TextUtils.isEmpty(leftButtonText)){
                mtextView.setText(leftButtonText);
                //设置左边按钮文字的颜色
                int lbtext=attributes.getColor(R.styleable.CustomTitleBar_left_button_text_color,Color.WHITE);
                mleftBtn.setTextColor(lbtext);
            }
            //设置左边图片icon 这里设置二选一 文字或是图片
            int leftIcon =attributes.getResourceId(R.styleable.CustomTitleBar_left_button_drawable,R.mipmap.ic_launcher);
            if(leftIcon!=-1){
                mleftBtn.setCompoundDrawablesRelativeWithIntrinsicBounds(leftIcon,0,0,0);//设置到控件的位置
            }
            //处理标题 先获取标题是否要显示图片icon
            int titleTextIcon=attributes.getResourceId(R.styleable.CustomTitleBar_title_text_drawable,-1);
            if(titleTextIcon!=-1){
                mtextView.setBackgroundResource(titleTextIcon);
            }else{
                //不是图片显示文字
                String titleText=attributes.getString(R.styleable.CustomTitleBar_title_text);
                if(!TextUtils.isEmpty(titleText)){
                    mtextView.setText(titleText);
                }
                //获取标题颜色
                int titltextColor= attributes.getColor(R.styleable.CustomTitleBar_title_text_color,Color.WHITE);
                mtextView.setTextColor(titltextColor);
            }
            //是否要显示右侧按钮
            boolean mrightBtnVisible= attributes.getBoolean(R.styleable.CustomTitleBar_right_button_visible,true);
            if(mrightBtnVisible){
                mrightBtn.setVisibility(View.VISIBLE);
            }else{
                mrightBtn.setVisibility(View.INVISIBLE);
            }

            // 设置右边图片icon 这里设置二选一 文字或是图片 同时设置时显示图片
            int rightButtonIcon =attributes.getResourceId(R.styleable.CustomTitleBar_right_button_drawable,-1);
            if(rightButtonIcon!=-1){
                mrightBtn.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,rightButtonIcon,0);
            }else{
                //按钮文字
                String rightText= attributes.getString(R.styleable.CustomTitleBar_right_button_text);
                if(!TextUtils.isEmpty(rightText)){
                    mrightBtn.setText(rightText);
                    //设置文字颜色
                    int rcolor =attributes.getColor(R.styleable.CustomTitleBar_right_button_text_color,Color.WHITE);
                    mrightBtn.setTextColor(rcolor);
                }
            }
            attributes.recycle();
        }
    }

    public void setTitleClickListener(OnClickListener listener){
        if(listener!=null){
            mleftBtn.setOnClickListener(listener);
            mrightBtn.setOnClickListener(listener);
        }
    }

    public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public Button getMleftBtn() {
        return mleftBtn;
    }

    public Button getMrightBtn() {
        return mrightBtn;
    }

    public TextView getMtextView() {
        return mtextView;
    }
    //    public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//    }
}
